package com.hospital.acctmanage.module.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.hospital.acctmanage.module.entity.SettleRequestEntity;
import com.hospital.acctmanage.module.service.OutpRcptMasterService;
import com.hospital.common.core.exception.ExceptionPrint;
import com.hospital.common.core.module.controller.vo.RefundBillVo;
import com.hospital.common.core.module.entity.WxPayEntity;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.ReturnResult;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 结算结账等前端控制器
 * </p>
 *
 * @author lvyao
 * @since 2021-09-15
 */
@RestController
@Validated
@Slf4j
@RequestMapping("/acct")
public class SettleAcctController {
    @Autowired
    private OutpRcptMasterService outpRcptMasterService;

    /**
     * 柜面结算并第三方支付
     *
     * @param params
     * @return
     */

    @RequestMapping("/settle")
    public Tip wxPayTransfer(@RequestBody String params, HttpServletResponse httpServletResponse) throws IOException {
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        String startTime = DateUtil.getAllTime();
        Tip tip = new SuccessTip(null);
        SettleRequestEntity settleRequestEntity;
        List<WxPayEntity> wxPayEntityList;
        String dbType;
        httpServletResponse.setCharacterEncoding("UTF-8");
        try {
            settleRequestEntity = JSON.parseObject(params, SettleRequestEntity.class);
//            dbType = DbContextHolder.getDbType();
//            settleRequestEntity.setHospitalCode(DataBaseValue.getDataBaseVaule(dbType));
            wxPayEntityList = settleRequestEntity.getSettlePatientInfo();

            ReturnResult settle;
            settle = outpRcptMasterService.settle(wxPayEntityList);
            if (!settle.isStatus()) {
                tip = new ErrorTip("1000", "处理结算数据失败");
            }
        } catch (JSONException json) {
            tip = new ErrorTip("1001", json.getMessage());
        } catch (ParseException json) {
            tip = new ErrorTip("1002", json.getMessage());
        } catch (RuntimeException e) {
            tip = new ErrorTip("1003", "处理结算数据失败！具体情况：" + e.getMessage());
        } catch (Exception e) {
            tip = new ErrorTip("1004", e.getMessage());
        } finally {
            stopwatch.stop();
            String endTime = DateUtil.getAllTime();
            ExceptionPrint.printCommErrorString("结算调用", JSON.toJSONString(params), String.valueOf(tip.getCode()), tip.getMessage(),
                    startTime, endTime, String.valueOf(stopwatch.getTotalTimeSeconds()));
        }
        return tip;
    }


    /**
     * 柜面结算并第三方支付
     *
     * @param params
     * @return
     */

    @RequestMapping("/resettle")
    public Tip refundSettle(@RequestBody @Valid String params, HttpServletResponse httpServletResponse) throws IOException {
        StopWatch stopwatch = new StopWatch();
        stopwatch.start();
        String startTime = DateUtil.getAllTime();
        Tip tip = new SuccessTip(null);
        RefundBillVo refundBillVo;
        httpServletResponse.setCharacterEncoding("UTF-8");
        try {
            refundBillVo = JSON.parseObject(params, RefundBillVo.class);
            ReturnResult settle;
            settle = outpRcptMasterService.refundSettle(refundBillVo);
            if (!settle.isStatus()) {
                return new ErrorTip("1000", "处理结算数据失败");
            }
        } catch (JSONException json) {
            tip = new ErrorTip("1001", json.getMessage());
        } catch (ParseException json) {
            tip = new ErrorTip("1002", json.getMessage());
        } catch (RuntimeException e) {
            tip = new ErrorTip("1003", "处理结算数据失败！具体情况：" + e.getMessage());
        } catch (Exception e) {
            tip = new ErrorTip("1004", "处理结算数据失败！具体情况：" + e.getMessage());
        } finally {
            stopwatch.stop();
            String endTime = DateUtil.getAllTime();
            ExceptionPrint.printCommErrorString("退费调用", JSON.toJSONString(params), String.valueOf(tip.getCode()), tip.getMessage(),
                    startTime, endTime, String.valueOf(stopwatch.getTotalTimeSeconds()));
        }
        return tip;

    }

}

